package com.xw.musicplayer.focus;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * 音频焦点辅助.
 *
 * @author ingxin
 * @date 2020/5/24
 */
public class AudioFocusHelper {

    private BaseAudioFocusHelper mAudioHelper;
    private TelephonyManager mTelephonyManager;
    private PhoneStateListener mPhoneStateListener;

    public AudioFocusHelper(Context context,AudioManager.OnAudioFocusChangeListener listener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mAudioHelper = new BaseAudioFocusHelper26(listener);
        } else {
            mAudioHelper = new BaseAudioFocusHelper21(listener);
        }

        startTelephonyListener(context,listener);
    }

    public void request(Context context) {
        mAudioHelper.request(context);
    }

    public void abandon() {
        mAudioHelper.abandon();
        stopTelephonyListener();
    }

    private void startTelephonyListener(Context context,AudioManager.OnAudioFocusChangeListener listener) {
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephonyManager == null) {
            Log.e("info", "Failed to initialize TelephonyManager!!!");
            return;
        }

        mPhoneStateListener = new PhoneStateListener() {

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.d("info", "PhoneStateListener: CALL_STATE_IDLE");
                        listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN);
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        Log.d("info", "PhoneStateListener: CALL_STATE_OFFHOOK");
                        listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        Log.d("info", "PhoneStateListener: CALL_STATE_RINGING: " + incomingNumber);
                        listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
                        break;
                    default:
                        break;
                }
            }
        };
        try {
            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopTelephonyListener() {
        if (mTelephonyManager != null && mPhoneStateListener != null) {
            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
            mTelephonyManager = null;
            mPhoneStateListener = null;
        }
    }
}
